# -*- encoding: utf-8 -*-
from django.conf import settings
from django.db import models
from django.template.defaultfilters import slugify
from django.utils.translation import ugettext_lazy as _
from django.utils.safestring import mark_safe

from multilingual.translation import TranslationModel
from filebrowser.fields import FileBrowseField
import re 

from modulo.page.models import Section

class Country(models.Model):
    code = models.CharField(_(u'code'), max_length=2, primary_key=True, help_text=_(u"Voir <a href='http://nephi.unice.fr/codes_iso_pays.php' target='_blank'>la liste des codes des pays</a>."))

    class Translation(TranslationModel):
        name = models.CharField(_(u'nom'), max_length=75)

    def __unicode__(self):
        return u'%s' % self.name

    def flag(self):
        return u"icones/flags/%s.png" % self.code

    class Meta:
        verbose_name = _(u'Pays')
        verbose_name_plural = _(u"Pays")

class Genre(models.Model):
    class Translation(TranslationModel):
        name = models.CharField(_(u'Nom'), max_length=30, unique=True)

    def __unicode__(self):
        return u'%s' % self.name

class Group(models.Model):
    section = models.ForeignKey(Section, related_name="groups",
                                limit_choices_to = {'app': 'groups'},
                                help_text=_(u"Selection de la rubrique contenant ce groupe."))

    genre = models.ForeignKey(Genre, related_name="groups")
    countries = models.ManyToManyField(Country, related_name="groups")

    code = models.CharField(_(u'Code du groupe'), max_length=5, help_text=_(u"Exemple C002 ou MA201"))
    photo = FileBrowseField(_(u'photo'), max_length=100, directory="groups", format='Image', help_text=_(u"Photo du groupe - attention de bien choisir la miniature de 640px."), blank=True)

    class Translation(TranslationModel):
        name = models.CharField(_(u'Nom'), max_length=100)
        comment = models.TextField(_(u'Description'), blank=True)

    class Meta:
        ordering = ('code',)

    def __unicode__(self):
        return u"%s : %s" % (self.code, self.name)

    def class_css(self):
        style_class = re.search('^[a-zA-Z]+', self.code)
        return style_class.group(0)

    def get_absolute_url(self):
        return u"%s%d/%s/" % (self.section.path(), self.id, slugify(self.name))
